﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_survey : System.Web.UI.Page
{
    myDataSetTableAdapters.SurveysTableAdapter surAdapter = new myDataSetTableAdapters.SurveysTableAdapter();
    myDataSetTableAdapters.SurveyCategoryTableAdapter surCateAdapter = new myDataSetTableAdapters.SurveyCategoryTableAdapter();
    myDataSetTableAdapters.Survey_QuestionTableAdapter surQuesAdapter = new myDataSetTableAdapters.Survey_QuestionTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            display();
            myDataSet.SurveyCategoryDataTable surCateTable = surCateAdapter.GetData();
            DropDownList1.AppendDataBoundItems = true;
            DropDownList1.DataSource = surCateTable.DefaultView;
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "id";
            DropDownList1.DataBind();
        }
    }

    protected void display()
    {
        myDataSet.SurveysDataTable surTable = surAdapter.GetDataConvert();
        GridView1.DataSource = surTable;
        GridView1.DataBind();
    }

    protected void clear()
    {
        txtId.Text = "";
        txtTitle.Text = "";
        txtDes.Text = "";
        DropDownList1.SelectedValue = "-1";
        RadioButtonList1.SelectedIndex = -1;
        RadDatePicker1.SelectedDate = null;
        lblResult.Text = null;
        lblTitleError.Visible = false;
        lblCateError.Visible = false;
        lblStatusError.Visible = false;
        lblDateError.Visible = false;
    }

    protected bool checkEmpty()
    {
        bool status = true;
        if (txtTitle.Text.Trim() == "")
        {
            lblTitleError.Visible = true;
            status = false;
        }
        else
        {
            lblTitleError.Visible = false;
        }
        if (DropDownList1.SelectedValue == "-1")
        {
            lblCateError.Visible = true;
            status = false;
        }
        else
        {
            lblCateError.Visible = false;
        }
        if (RadioButtonList1.SelectedIndex == -1)
        {
            lblStatusError.Visible = true;
            status = false;
        }
        else
        {
            lblStatusError.Visible = false;
        }
        if (RadDatePicker1.SelectedDate == null)
        {
            lblDateError.Visible = true;
            status = false;
        }
        else
        {
            lblDateError.Visible = false;
        }
        return status;
    }

    protected void cmdSearchEnable_Click(object sender, EventArgs e)
    {
        display();
        PanelInvi1.Visible = false;
        txtId.Enabled = true;
        txtId.BackColor = System.Drawing.Color.White;
        cmdSearch.Text = "Search";
        cmdClear.Visible = true;
        clear();
        if (cmdSearchEnable.Text == "Enable Search Area")
        {
            Panel1.Visible = true;
            cmdSearchEnable.Text = "Disable Search Area";
            cmdAddEnable.Text = "Enable Add Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdSearchEnable.Text = "Enable Search Area";
        }
    }

    protected void cmdAddEnable_Click(object sender, EventArgs e)
    {
        display();
        PanelInvi1.Visible = true;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Add new";
        cmdClear.Visible = true;
        clear();
        if (cmdAddEnable.Text == "Enable Add Area")
        {
            Panel1.Visible = true;
            cmdAddEnable.Text = "Disable Add Area";
            cmdSearchEnable.Text = "Enable Search Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdAddEnable.Text = "Enable Add Area";
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        if (cmdSearch.Text == "Search")
        {
            try
            {
                Panel1.Visible = true;
                string strSQL = "SELECT Surveys.id, Surveys.title, Surveys.description, Surveys.category_id, Surveys.is_active, Surveys.created_at, SurveyCategory.name, CASE Surveys.is_active WHEN 1 THEN 'Active' ELSE 'Inactive' END AS status, CONVERT(varchar(10), Surveys.created_at, 103) AS date FROM Surveys INNER JOIN SurveyCategory ON Surveys.category_id = SurveyCategory.id where";
                strSQL += " Surveys.title like N'%" + txtTitle.Text.Trim() + "%'";
                if (DropDownList1.SelectedValue != "-1")
                {
                    strSQL += " and Surveys.category_id = " + DropDownList1.SelectedValue.ToString();
                }
                if (RadioButtonList1.SelectedIndex != -1)
                {
                    strSQL += " and Surveys.is_active = '" + RadioButtonList1.SelectedValue.ToString() + "'";
                }
                if (RadDatePicker1.SelectedDate != null)
                {
                    strSQL += " and CONVERT(varchar(10), Surveys.created_at, 103) like '" + string.Format("{0:dd/MM/yyyy}", RadDatePicker1.SelectedDate.Value) + "'";
                }
                if (txtId.Text.Trim() != "")
                {
                    strSQL += " and Surveys.id = " + txtId.Text.Trim();
                }
                string strConnection = ConfigurationManager.ConnectionStrings["EnvironmentSurveyConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(strConnection);
                SqlCommand sqlCmd = new SqlCommand(strSQL, conn);
                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                if (dt.Rows.Count != 0)
                {
                    if (dt.Rows.Count == 1)
                    {
                        lblResult.Text = "There is 1 result";
                    }
                    else
                    {
                        lblResult.Text = "There are " + dt.Rows.Count + " results";
                    }
                }
                else
                {
                    lblResult.Text = "0 result... Please search again!";
                }
            }
            catch
            {
                RegisterStartupScript("as", "<script>alert('Input wrong format!')</script>");
            }
            
        }
        else if (cmdSearch.Text == "Add new")
        {
            if (checkEmpty())
            {
                surAdapter.InsertQuery(txtTitle.Text.Trim(), txtDes.Text.Trim(), int.Parse(DropDownList1.SelectedValue.ToString()), bool.Parse(RadioButtonList1.SelectedValue.ToString()), RadDatePicker1.SelectedDate.Value);
                display();
            }
        }
        else
        {
            if (checkEmpty())
            {
                surAdapter.UpdateQuery(txtTitle.Text.Trim(), txtDes.Text.Trim(), int.Parse(DropDownList1.SelectedValue.ToString()), bool.Parse(RadioButtonList1.SelectedValue.ToString()), RadDatePicker1.SelectedDate.Value,int.Parse(txtId.Text.Trim()));
                display();
            }
        }
    }

    protected void cmdClear_Click(object sender, EventArgs e)
    {
        clear();
        display();
        Panel1.Visible = true;
    }

    protected void cmdHide_Click(object sender, EventArgs e)
    {
        display();
        Panel1.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        lblResult.Text = null;
    }

    protected void getDataById(int id)
    {
        myDataSet.SurveysDataTable surTable = surAdapter.GetDataById(id);
        txtId.Text = surTable[0]["id"].ToString();
        txtTitle.Text = surTable[0]["title"].ToString();
        txtDes.Text = surTable[0]["description"].ToString();
        DropDownList1.SelectedValue = surTable[0]["category_id"].ToString();
        RadioButtonList1.SelectedValue = surTable[0]["is_active"].ToString();
        RadDatePicker1.SelectedDate = DateTime.Parse(surTable.Rows[0]["created_at"].ToString());
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        PanelInvi1.Visible = true;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Save";
        cmdClear.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        clear();
        getDataById(id);
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            surAdapter.DeleteQuery(id);
            clear();
            display();
        }
        catch
        {
            RegisterStartupScript("as", "<script>alert('Cannot delete!')</script>");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        display();
    }
}
